package com.century.sjt.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description: 余额
 * @date : 2016/8/20 14:35
 */
public class WalletBalanceActivity extends BaseActivity {
    private TextView tvBalance;
    private Button btnRecharge;
    private Button btnConsume;
    private RequestQueue mQueue;
    private String isShop, isMerchant;
    TextView balance_tv_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_balance);
        mQueue = Volley.newRequestQueue(this);
        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        isShop = mainSharedPreferences.getString("isShop", "");
        isMerchant = mainSharedPreferences.getString("isMerchant", "");
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        queryBalance();
    }


    @Override
    protected void initView() {
        tvBalance = (TextView) findViewById(R.id.tv_wb_balance);
        btnRecharge = (Button) findViewById(R.id.btn_wb_recharge);
        btnConsume = (Button) findViewById(R.id.btn_wb_consume);
        balance_tv_icon= (TextView) findViewById(R.id.balance_tv_icon);
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        balance_tv_icon.setTypeface(iconfont);

        balance_tv_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(WalletBalanceActivity.this, WalletRechareActivity.class);
                startActivity(intent);

            }
        });

        btnConsume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(WalletBalanceActivity.this, WalletToCosumeActivity.class);
                startActivity(intent);
            }
        });
        if ("1".equals(isShop) || "1".equals(isMerchant)) {
            btnRecharge.setVisibility(View.GONE);
        }


    }

    /**
     * 获取余额
     */

    private void queryBalance() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), WalletBalanceActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.QueryBalanceHost, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("获取用户余额成功", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
                        JSONObject data = res.getJSONObject("data");
                        String amount = data.getString("amount");
                        Log.e("amount",String.format("{0:N}",amount));
                        tvBalance.setText(amount);
                    } else {//失败
                        TipUtil.showToast(message, WalletBalanceActivity.this, 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), WalletBalanceActivity.this, 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), WalletBalanceActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }


    @Override
    protected void onDestroy() {
        mQueue.cancelAll(this);
        super.onDestroy();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }
}
